#!/usr/bin/perl 
#
# (C) Copyright 2010- Hewlett-Packard Development Company, L.P.
#
# $Id: instance.pm 17950 2012-05-08 07:47:06Z cheng-xin.cai@hp.com $
#

package bright;
require Exporter;

our @ISA = qw(Exporter);
our %EXPORT_TAGS = ('all' => [qw(
    cm_Check
    cm_GetVersion
    cm_IsHeadNode
    cm_AddExcludeList
    cm_RemoveExcludeList
    cm_ImageUpdate
    cm_UpdateProvisioners
    cm_GetClusterName
    cm_IsNodeUp
    cm_GetNodeGroup
    cm_IsValidUser
    cm_GetCategories
    cm_GetImageNames
    cm_GetHeadnodes
 
)]);
our @EXPORT_OK = (@{$EXPORT_TAGS{'all'}});

use 5.008000;
use strict;
use warnings;

use POSIX;
use Data::Dumper qw/Dumper/;
use File::Basename;
use File::Copy;
use Sys::Hostname;

use lib '/opt/hp/nv/lib/perl';
use config qw/instance_log_event/;
 
use constant CRITICAL => 2; 
my $TRUE = 1;
my $FALSE = 0;

my $cmsh_cmd = "/cm/local/apps/cmd/bin/cmsh";
my $service_cmd = "/sbin/service";

sub cm_Check() 
{
    print "$service_cmd cmd status 2>&1\n";
    print "$cmsh_cmd -c 'help' 2>&1\n";

    return $TRUE;
}

sub cm_GetVersion
{

    print "egrep -o v[0-9\\.]+ /etc/cm-release | egrep -o [0-9\\.]+\n";

    return 5.1;
}


sub cm_IsHeadNode
{
    return 1;
}

sub cm_AddExcludeList
{
    my $category = shift;
    my $listname = shift;
    my @locations = @_;
    
    print "add $category - $listname: @locations\n";

    return $TRUE;
}

sub cm_RemoveExcludeList
{
    my $category = shift;
    my $listname = shift;
    my @locations = @_;

    print "remove $category - $listname: @locations\n";    

    return $TRUE;
}

sub cm_ImageUpdate($)
{
    my $noderange = shift;

    print "$cmsh_cmd -c \"device ;imageupdate -w $noderange\"\n";
    
    return $TRUE;
}

sub cm_UpdateProvisioners
{
    print "$cmsh_cmd -c 'softwareimage ; updateprovisioners'\n";
    
    return $TRUE;
}

sub cm_GetClusterName
{
    return hostname();
}

sub cm_IsNodeUp($)
{
    my $node = shift;
    print "$cmsh_cmd -c \"ds $node\"\n";

    return $TRUE;
}

sub cm_GetNodeGroup
{
    print "$cmsh_cmd -c 'nodegroup list' | grep ^rack\n";
    my @groups = ("n001..n010", "n011..n021", "n022..n032", "n033..n043");
    
    return wantarray ? @groups : join(',', @groups);
}

sub cm_IsValidUser($)
{
    my $user = shift;
    print "$cmsh_cmd -c 'user show user $user' >/dev/null 2>&1\n";

    return -d "/home/$user";
}

sub cm_GetCategories
{
    return ("neoview");
}

sub cm_GetImageNames
{
    return ("nv-image");
}

sub cm_GetHeadnodes
{
    return ("atcsq01-hn1", "atcsq01-hn2", "cxcai-X220i", "CCAI");
}

1;

